<?php
/*
 *用户管理
 *author 王建 
 */

class MemberAction extends BaseAction{
    public $d = '';
    public $default_field = array(
        'a.uid'=>'用户UID',
        'a.username'=>'用户名',
        'a.phone'=>'手机号码',
    	'a.uname'=>'昵称' ,
    ); //默认字段
    public $all_field = array(
        'a.uid'=>'用户UID',
        'a.username'=>'用户名',
        'a.phone'=>'手机号码',
        'a.uname'=>'昵称' ,
        'a.sex'=>'性别',
        'a.status'=>'状态',
        'a.regdate'=>'注册日期',
        'a.expire'=>'有效期',
        'a.score'=>"积分",
        'a.money' =>"金币钱",
        'a.sign_days'=>'连续签到天数',
        'a.ip'=>'注册ip',
    	'a.alipay_name'=>'支付宝姓名',
    	'a.alipay_account'=>'支付宝帐号',
    	'a.pic'=>'用户图像',
    ) ; //查询的所有字段
    public $must_field = array(
        'a.uid'=>'用户UID',
        'a.username'=>'用户名',
        'a.phone'=>'手机号码',
    ) ;
    public $cache_file = "";
    function __construct(){
        parent::__construct();
        $this->d = D("Admin/Member");;
        $this->cache_file = "member_list_".$this->uid;

    }
    function index(){
        $action = $this->_request("action");    
        $action_array = array("show","ajax_data" ,"setField");
        $action = !in_array($action,$action_array)?'show':$action ;
        if($action == 'show'){
        	$cache =  S($this->cache_file);
        	$cache_field_array = array() ;
        	$cache_field = array();
        	if($cache){
        		$cache_field_array = @unserialize($cache);
        		$cache_field = @implode(",", $cache_field_array);
        	}
        	$this->assign("all_field" , $this->all_field);
        	$this->assign("cache_field" , $cache_field);
        	$this->assign("cache_field_array" , $cache_field_array);
            $this->assign("all_field" , $this->all_field);
            $this->display();
        }elseif($action == 'ajax_data'){
            $this->ajax_data();
        }elseif($action == 'setField' ){
            $this->saveFieldCache();
        }
    }
    //ajax get data
    private function ajax_data(){
        $page = intval($this->_request("page"  ));
        $list = $this->d->queryMemberList($page , 
                $this->get_search_params() ,$this->must_field , $this->default_field , $this->all_field);

        echo result_to_towf_new($list['list'], 1, '成功', $list['page_string']) ;
    }
    //获取搜索的条件参数 ， 返回数组
    public function get_search_params(){
        return $search = array(
                            'username'=>trim($this->_request("username")) ,
                            'uname'=>trim($this->_request("uname")) ,
                            'status'=>trim($this->_request("status")) ,
                            'phone'=>trim($this->_request("phone")) ,
                            'sex'=>trim($this->_request("sex")) ,
                            'status'=>trim($this->_request("status")) ,
                            'condition'=>trim($this->_request("condition")) ,
                            'show_field'=>trim($this->_request("show_field")) ,
                            'field'=>trim($this->_request("field")) ,
                            'orderby'=>trim($this->_request("orderby")) ,
                            'uid'=>trim($this->_request("uid")) ,
                            'pageSize'=>trim($this->_request("pageSize")) ,
                ) ;
    }
    //
    //把用户设置的字段到缓存文件里面
    private function saveFieldCache(){
        $show_field = trim($this->_request("show_field"));
        $field = $this->default_field ;
        if(empty($show_field)){
            $field = array_keys($field);
        }else{
            $field = explode(",", $show_field) ;
        }
        $field = serialize($field) ;
        S($this->cache_file,$field,C("field_expire"),'File');
        echo result_to_towf_new(null, 1, 'success', null) ;
    }
    //编辑页面
    function edit(){
        $action = $this->_request("action");        
        $action_array = array("edit","doedit" );
        $action = !in_array($action,$action_array)?'edit':$action ;     
        if($action == 'edit'){
            $id = intval($this->_request("id"));
            $info = $this->d->getMemberDataById($id);
            $this->assign("ad_type" , $type_array);
            $this->assign("info" , $info) ;
            $this->assign("id" , $id) ;
            $this->display();       
        }elseif($action == 'doedit'){
            $this->doedit();
        }

    }
    //处理编辑数据
    private function doedit(){
        $res = $this->d->editMember();
        if($res['status']){
            echo result_to_towf_new(null, 1, $res['message'], null);
        }else{
            echo result_to_towf_new(null, 0, $res['message'], null);
            exit ;
        }
    }
    //用户统计
    public function count(){
        $action = $this->_request("action");        
        $action_array = array("show","ajax_data" );
        $action = !in_array($action,$action_array)?'show':$action ;     
        if($action == 'show'){
            $this->display();       
        }elseif($action == 'ajax_data'){
            $this->count_ajax();
        }
    }
    function count_ajax(){
        $page = intval($this->_request("page"  ));
        $list = $this->d->queryMemberCount($page,10,
                array(
                    'begin_date'=>trim($this->_request("begin_date")) ,
                    'end_date'=>trim($this->_request("end_date")) ,
                    'status'=>trim($this->_request("status")) ,
                    'count_type'=>trim($this->_request("count_type")) ,
                    'sex'=>trim($this->_request("sex")) ,
                    'status'=>trim($this->_request("status")) ,
                ) );

        echo result_to_towf_new($list['list'], 1, '成功', $list['page_string']) ;
    }
    //用户导出
    function export(){
        $page = intval($this->_request("page"  ));
        $list = $this->d->export($page , 
        $this->get_search_params() ,$this->must_field , $this->default_field , $this->all_field);
    }  
}
//file end 